home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 686 / multibox / multibox.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  9KB  |  271 lines

  1. /*
  2.  *    © 1992 by Michael Watzl, compiled with DICE: dcc multibox.c -2.0 -o Multibox
  3.  */
  4.  
  5.  
  6. #include <exec/memory.h>
  7. #include <intuition/intuitionbase.h>
  8. #include <intuition/intuition.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <graphics/gfx.h>
  11. #include <graphics/Text.h>
  12. #include <libraries/gadtools.h>
  13. #include <dos/rdargs.h>
  14. #include <string.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/dos_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/graphics_protos.h>
  20. #include <clib/gadtools_protos.h>
  21.  
  22. #define ACTIVESCREEN    IntuitionBase->ActiveScreen
  23. #define FHEIGHT        ACTIVESCREEN->Font->ta_YSize
  24. #define WHEIGHT        (8*FHEIGHT+2*boxnumber*FHEIGHT+3)
  25. #define GYOFFSET    (FHEIGHT*3.6)
  26. #define GXOFFSET    pixwidth((UBYTE *)"     ")
  27. #define CTOP        WHEIGHT-3*FHEIGHT+3
  28.  
  29. /*
  30.  *    Protos
  31.  */
  32.  
  33. VOID    quit( STRPTR errtext, LONG rc);
  34. WORD    pow( WORD base, exp);
  35. VOID    cleanquit( VOID );
  36. VOID    GetIDCMP( VOID );
  37.  
  38. VOID    usage( VOID );
  39. VOID    calcwidths( VOID );
  40. LONG    pixwidth( STRPTR text );
  41.  
  42. VOID    exit( LONG rc );
  43.  
  44. struct IntuitionBase     *IntuitionBase;
  45. struct GfxBase         *GfxBase;
  46. struct GadToolsBase     *GadToolsBase;
  47. struct Window         *window;
  48. struct IntuiMessage     *message;
  49. struct Gadget         *congad;            /* pointer to gadgetcontext (list) */
  50. struct TextExtent    textextent;
  51.  
  52. struct NewGadget CancelGad= {100,100,40,10,(UBYTE *)"CANCEL!",NULL, 0,PLACETEXT_IN,NULL,NULL};
  53. struct NewGadget OkGad    = {100,100,40,10,(UBYTE *)"OK!"    ,NULL,20,PLACETEXT_IN,NULL,NULL};
  54. struct RDArgs *cliargs;
  55. struct {
  56.         STRPTR  Hail;
  57.         STRPTR    Title;
  58.         STRPTR    *Box;
  59. }    array;
  60. struct IntuiText iText = {1,0,JAM1,0,0,NULL,NULL,NULL};
  61. void    *vi;                        /* var for VisualInfo */
  62. char    *boxtext[20];
  63. WORD    boxnumber,textwidth,wwidth,cwidth;        /* number of chackbox-gadgets, max textwidth, width of req-window, width of cancelgadget */
  64.  
  65. VOID
  66. quit( STRPTR errtxt, LONG rc)                /* print errortext, clean up and put returncode */
  67. {
  68.     if(errtxt)         PutStr(errtxt);
  69.     if(window)         CloseWindow(window);
  70.     if(congad)         FreeGadgets(congad);
  71.     if(vi)            FreeVisualInfo(vi);
  72.     if(GadToolsBase)     CloseLibrary(GadToolsBase);
  73.     if(GfxBase)         CloseLibrary(GfxBase);
  74.     if(IntuitionBase)     CloseLibrary((struct Library*)IntuitionBase);
  75.     if(cliargs)        FreeArgs( cliargs );
  76.     exit(rc);
  77. }
  78.  
  79. WORD
  80. pow( WORD base, exp )                    /* calcualte  a power b */
  81. {
  82.     WORD i=0,po=1;
  83.     for(i=0;i<exp;i++)
  84.         po*=base;
  85.     return(po);
  86. }
  87.  
  88. VOID
  89. cleanquit( VOID )                    /* calculate the returncode (selected/notselected) boxes */
  90. {
  91.     struct Gadget *g;
  92.     LONG i,rc=0;
  93.  
  94.     g=congad;        /* FirstGadget */
  95.     for(i=0;i<=boxnumber;i++){                    /* claculate the RC */
  96.         if( (g->Flags) & GFLG_SELECTED) rc+=pow(2,i-1);
  97.         g=g->NextGadget;
  98.     }
  99.     quit(NULL , rc);
  100. }
  101.  
  102. VOID
  103. GetIDCMP( VOID )
  104. {
  105.     WORD     id;
  106.     struct    Gadget *g;
  107.     UBYTE    code;
  108.     ULONG    class=0;
  109.  
  110.     FOREVER
  111.     {
  112.         Wait(1<<window->UserPort->mp_SigBit);
  113.         while (message=GT_GetIMsg(window->UserPort))
  114.         {
  115.             class=message->Class;
  116.             if(class==IDCMP_CLOSEWINDOW)
  117.                 quit(NULL,0);
  118.             if(class==IDCMP_GADGETUP){
  119.                 g=(struct Gadget*)message->IAddress;
  120.                 id=g->GadgetID;
  121.                 if(id==0)  quit(NULL,65536);    /* cancel Gadget */
  122.                 if(id==20) cleanquit();        /* ok gadget */
  123.             }
  124.             if(class==IDCMP_VANILLAKEY){
  125.                 code=message->Code;
  126.                 if(code==13) cleanquit();
  127.             }
  128.             GT_ReplyIMsg(message);
  129.         } /* while message */
  130.     } /* Forever */
  131. }
  132.  
  133. VOID
  134. usage( VOID )
  135. {
  136.     PutStr("Usage: multibox [\"Hailstring\"] TITLE \"TitleString\" BOXES Box1|Box2|...\n");
  137.     PutStr("© 1992 by Michael Watzl \n");
  138.     PutStr("\nThe returncode inicates which gadgets have been selected.\n");
  139.     PutStr("Example:\n The first an third gadged have been selected\n => You'll get the rc: 1*2^0 + 0*2^1 + 1*2^2 = 5\n");
  140.     exit(5);
  141. }
  142.  
  143. main(WORD argc, STRPTR *argv)
  144. {
  145.     WORD    i=0;
  146.     struct    Gadget *gad;
  147.     struct    TagItem CheckGadTags[2] = {GTCB_Checked,FALSE,TAG_DONE,0};
  148.     STRPTR    template = "HAIL/K,TITLE/K/A,BOXES/F/A", arg;
  149.     const    STRPTR  ws ="|";
  150.     struct    NewGadget **ng;
  151.  
  152.            if(!( cliargs=ReadArgs( template , &array, NULL))){
  153.         FreeArgs(cliargs);
  154.         usage();
  155.     }
  156.  
  157.     for( arg = strtok( array.Box , ws ) ; arg ; arg=strtok(NULL,ws) ) boxtext[i++]=arg;
  158.     boxnumber=i;
  159.  
  160.     if(!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L)))
  161.         quit("Clould not open intuition.library V37!\n",10);
  162.     if(!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L)))
  163.         quit("Clould not open graphics.library V37!\n",10);
  164.     if(!(GadToolsBase=(struct GadToolsBase *)OpenLibrary("gadtools.library",0L)))
  165.         quit("Clould not open gadtools.library!\n",10);
  166.     if(!(vi=(void *)GetVisualInfoA(ACTIVESCREEN,NULL)))
  167.         quit("Can't get visual info!\n",10);
  168.     if(!(gad=(struct Gadget *)CreateContext((struct Gadget **)&congad)))
  169.         quit("Can't create context!\n",10);
  170.  
  171.     calcwidths();                    /* calc width of window, gadgets */
  172.  
  173.     if(!( ng[0] = AllocMem( sizeof( struct NewGadget ) * boxnumber , MEMF_PUBLIC | MEMF_CLEAR ))) quit("Can't allocate enough memory!\n",10);
  174.     for(i=0;i<boxnumber;i++)
  175.     {
  176.         ng[i]->ng_LeftEdge   = GXOFFSET;
  177.         ng[i]->ng_TopEdge    = GYOFFSET+ (2*i+1)*FHEIGHT;
  178.         ng[i]->ng_GadgetText = boxtext[i];
  179.         ng[i]->ng_TextAttr   = ACTIVESCREEN->Font;
  180.         ng[i]->ng_GadgetID   = i+1;
  181.         ng[i]->ng_Flags      = PLACETEXT_RIGHT;
  182.         ng[i]->ng_VisualInfo = vi;
  183.  
  184.         if(boxtext[i][strlen(boxtext[i])-1]=='_')
  185.         {
  186.             boxtext[i][strlen(boxtext[i])-1]='\0';            /* Kill last char */
  187.             CheckGadTags[0].ti_Data=TRUE;
  188.         } else  CheckGadTags[0].ti_Data=FALSE;
  189.         if(!(gad = CreateGadgetA( CHECKBOX_KIND , gad , ng[i] , CheckGadTags ))){
  190.                 FreeMem( ng , sizeof( struct NewGadget ) * boxnumber );
  191.                 quit("Can't create gadget!\n",10);
  192.         }
  193.     }
  194.     FreeMem( ng[0] , sizeof( struct NewGadget ) * boxnumber );
  195.  
  196.     CancelGad.ng_LeftEdge=wwidth-cwidth-15;
  197.     CancelGad.ng_TopEdge=CTOP;
  198.     CancelGad.ng_Width =cwidth-3;
  199.     CancelGad.ng_Height=FHEIGHT*2;
  200.     CancelGad.ng_VisualInfo=vi;
  201.     CancelGad.ng_TextAttr=ACTIVESCREEN->Font;
  202.  
  203.     OkGad.ng_LeftEdge=15;
  204.     OkGad.ng_TopEdge=CTOP;
  205.     OkGad.ng_Width =cwidth-3;            /* Width minus  BevelBoxSize */
  206.     OkGad.ng_Height=FHEIGHT*2;
  207.     OkGad.ng_VisualInfo=vi;
  208.     OkGad.ng_TextAttr=ACTIVESCREEN->Font;
  209.  
  210.     if(!(gad=CreateGadget(BUTTON_KIND,gad,&CancelGad,GA_RelVerify,TRUE,TAG_DONE,0)))
  211.             quit("Can't create cancel gadget!\n",10);
  212.     if(!(gad=CreateGadget(BUTTON_KIND,gad,&OkGad,GA_RelVerify,TRUE,TAG_DONE,0)))
  213.             quit("Can't create ok gadget!\n",10);
  214.  
  215.     if(!(window=(struct Window *)OpenWindowTags(NULL,
  216.         WA_Top        ,ACTIVESCREEN->MouseY-((boxnumber+2)*FHEIGHT+50)/2,
  217.         WA_Left        ,ACTIVESCREEN->MouseX-(textwidth+200)/2,
  218.         WA_Width    ,wwidth,
  219.         WA_Height    ,WHEIGHT+5,
  220.         WA_Flags    ,WFLG_DRAGBAR|WFLG_CLOSEGADGET|WFLG_DEPTHGADGET|WFLG_ACTIVATE,
  221.         WA_IDCMP    ,IDCMP_CLOSEWINDOW|IDCMP_GADGETUP|IDCMP_VANILLAKEY,
  222.         WA_Gadgets    ,congad,
  223.         WA_Title    ,array.Hail,
  224.         WA_CustomScreen    ,ACTIVESCREEN,
  225.         TAG_DONE    ,0)))
  226.             quit("Could not open window!\n",10);
  227.  
  228.     DrawBevelBox(window->RPort,15-3              ,CTOP-2,cwidth+3,FHEIGHT*2+4,GT_VisualInfo,vi,TAG_DONE,0);
  229.     DrawBevelBox(window->RPort,wwidth-cwidth-15-3,CTOP-2,cwidth+3,FHEIGHT*2+4,GTBB_Recessed,TRUE,GT_VisualInfo,vi,TAG_DONE,0);
  230.  
  231.     iText.IText=array.Title;        /* Outlined looks much better! */
  232.     iText.ITextFont=ACTIVESCREEN->Font;
  233.     iText.FrontPen=1;
  234.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2-1,2+2*FHEIGHT-1);
  235.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2  ,2+2*FHEIGHT-1);
  236.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2+1,2+2*FHEIGHT-1);
  237.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2-1,2+2*FHEIGHT);
  238.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2+1,2+2*FHEIGHT);
  239.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2-1,2+2*FHEIGHT+1);
  240.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2  ,2+2*FHEIGHT+1);
  241.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2+1,2+2*FHEIGHT+1);
  242.     iText.FrontPen=3;
  243.     PrintIText(window->RPort,&iText,(wwidth-pixwidth(array.Title))/2,2+2*FHEIGHT);
  244.     DrawBevelBox(window->RPort,(wwidth-pixwidth(array.Title))/2-5,2+2*FHEIGHT-5,pixwidth(array.Title)+10,FHEIGHT+10,GT_VisualInfo,vi,TAG_DONE,0);
  245.  
  246.     GetIDCMP();
  247. }
  248.  
  249. VOID
  250. calcwidths( VOID )
  251. {
  252.  
  253.     WORD i,len;
  254.  
  255.     cwidth = pixwidth( "CANCEL!"  ) + 20;
  256.     len    = pixwidth( boxtext[0] );
  257.     for( i=1 ; i<boxnumber ; i++ )                    /* find longest string */
  258.         if( len < pixwidth( boxtext[i] ) ) len = pixwidth( boxtext[i] );
  259.     if( pixwidth( array.Title ) > len) len=pixwidth( array.Title );
  260.     textwidth = len + 20;
  261.     wwidth    = GXOFFSET + 30 + textwidth;
  262.     if( 2*cwidth+40>wwidth ) wwidth = 2*cwidth+40;
  263. }
  264.  
  265. LONG
  266. pixwidth( STRPTR text)                    /* calc width of text */
  267. {
  268.         TextExtent( &(ACTIVESCREEN->RastPort) , text , strlen(text) , &textextent );
  269.     return( textextent.te_Width );
  270. }
  271.